﻿using System;
using System.Collections.Generic;
using System.Text;
//new
using System.IO;
using System.Threading;
using DataRecv.enums;

namespace DataRecv.tools
{
    /// <summary>
    /// 磁盘清理工具，适用于按照/路口/年/月/日/xxx.jpg保存的磁盘空间清理
    /// </summary>
    public class DiskRecyle
    {
        /// <summary>
        /// 清理定时器
        /// </summary>
        private Timer m_freetimer;
        /// <summary>
        /// 保存天数
        /// </summary>
        private int m_keepdays;
        /// <summary>
        /// 保存路径，路口级文件夹
        /// </summary>
        private string m_path;
        /// <summary>
        /// 子文件夹模式
        /// </summary>
        private Enum_SubFolderMode m_mode;

        public DiskRecyle(string path, int keepdays, Enum_SubFolderMode foldermode)
        {
            m_path = path;
            m_keepdays = keepdays;
            m_mode = foldermode;
            m_freetimer = new Timer(new TimerCallback(TimerCallbackFunc), null, 0, 24 * 60 * 60 * 1000);
        }

        /// <summary>
        /// 销毁
        /// </summary>
        public void Dispose()
        {
            try
            {
                if (m_freetimer != null)
                {
                    m_freetimer.Dispose();
                }
                GC.Collect();
            }
            catch { }
        }

        private void TimerCallbackFunc(object obj)
        {
            try
            {
                switch (m_mode)
                { 
                    case Enum_SubFolderMode.地点代码_年_月_日:
                        ThreadPool.QueueUserWorkItem(new WaitCallback(DoDiscRecyleA), null);
                        break;
                    case Enum_SubFolderMode.路口名称_年_月_日:
                        ThreadPool.QueueUserWorkItem(new WaitCallback(DoDiscRecyleB), null);
                        break;
                    case Enum_SubFolderMode.年月日_路口名称:
                        ThreadPool.QueueUserWorkItem(new WaitCallback(DoDiscRecyleC), null);
                        break;
                    default:
                        break;
                }
            }
            catch { }
        }

        /// <summary>
        /// 删除过期文件夹和文件(路径规则必须为：/路口代码/年/月/日)
        /// 删除时找到日文件夹，如果过期，删除旗下所有图片和空文件夹
        /// </summary>
        private void DoDiscRecyleA(object obj)
        {
            try
            {
                TimeSpan fileKeepDays = TimeSpan.FromDays(m_keepdays);
                DateTime thistime = DateTime.Now;

                if ((m_path != "") && (Directory.Exists(m_path)))
                {
                    DirectoryInfo di_path = new DirectoryInfo(m_path);
                    DirectoryInfo[] di_roadids = di_path.GetDirectories();
                    if (di_roadids != null)
                    {
                        foreach (DirectoryInfo di_roadid in di_roadids)
                        {
                            #region try each roadid
                            try
                            {
                                DirectoryInfo[] di_yyyys = di_roadid.GetDirectories();
                                if (di_yyyys != null)
                                {
                                    foreach (DirectoryInfo di_yyyy in di_yyyys)
                                    {
                                        #region try each yyyy
                                        try
                                        {
                                            DirectoryInfo[] di_mms = di_yyyy.GetDirectories();
                                            if (di_mms != null)
                                            {
                                                foreach (DirectoryInfo di_mm in di_mms)
                                                {
                                                    #region try each mm
                                                    try
                                                    {
                                                        DirectoryInfo[] di_dds = di_mm.GetDirectories();
                                                        if (di_dds != null)
                                                        {
                                                            foreach (DirectoryInfo di_dd in di_dds)
                                                            {
                                                                #region try each dd
                                                                try
                                                                {
                                                                    string strtime = di_yyyy.Name + "-" + di_mm.Name + "-" + di_dd.Name + " 00:00:00";
                                                                    DateTime time = DateTime.Parse(strtime);
                                                                    if (thistime.Subtract(time).CompareTo(fileKeepDays) == 1)
                                                                    {
                                                                        try
                                                                        {
                                                                            di_dd.Delete(true);//删除day
                                                                            if (LogTool.gWorkEnable) LogTool.log.Info("磁盘空间回收:" + di_dd.FullName);
                                                                        }
                                                                        catch { }
                                                                    }
                                                                }
                                                                catch { }
                                                                #endregion
                                                            }
                                                        }

                                                    }
                                                    catch { }

                                                    try
                                                    {
                                                        di_mm.Delete();//删除空月
                                                    }
                                                    catch { }
                                                    #endregion
                                                }
                                            }
                                        }
                                        catch { }
                                        try
                                        {
                                            di_yyyy.Delete();//删除空年
                                        }
                                        catch { }
                                        #endregion
                                    }
                                }
                            }
                            catch { }
                            #endregion
                        }
                    }
                }
            }
            catch
            {
                ;
            }

        }

        /// <summary>
        /// 删除过期文件夹和文件(路径规则必须为：/路口/年/月/日)
        /// 删除时找到日文件夹，如果过期，删除旗下所有图片和空文件夹
        /// </summary>
        private void DoDiscRecyleB(object obj)
        {
            try
            {
                TimeSpan fileKeepDays = TimeSpan.FromDays(m_keepdays);
                DateTime thistime = DateTime.Now;

                if ((m_path != "") && (Directory.Exists(m_path)))
                {
                    DirectoryInfo di_path = new DirectoryInfo(m_path);
                    DirectoryInfo[] di_roads = di_path.GetDirectories();
                    if (di_roads != null)
                    {
                        foreach (DirectoryInfo di_road in di_roads)
                        {
                            #region try each road
                            try
                            {
                                DirectoryInfo[] di_yyyys = di_road.GetDirectories();
                                if (di_yyyys != null)
                                {
                                    foreach (DirectoryInfo di_yyyy in di_yyyys)
                                    {
                                        #region try each yyyy
                                        try
                                        {
                                            DirectoryInfo[] di_mms = di_yyyy.GetDirectories();
                                            if (di_mms != null)
                                            {
                                                foreach (DirectoryInfo di_mm in di_mms)
                                                {
                                                    #region try each mm
                                                    try
                                                    {
                                                        DirectoryInfo[] di_dds = di_mm.GetDirectories();
                                                        if (di_dds != null)
                                                        {
                                                            foreach (DirectoryInfo di_dd in di_dds)
                                                            {
                                                                #region try each dd
                                                                try
                                                                {
                                                                    string strtime = di_yyyy.Name + "-" + di_mm.Name + "-" + di_dd.Name + " 00:00:00";
                                                                    DateTime time = DateTime.Parse(strtime);
                                                                    if (thistime.Subtract(time).CompareTo(fileKeepDays) == 1)
                                                                    {
                                                                        try
                                                                        {
                                                                            di_dd.Delete(true);//删除day
                                                                            
                                                                            if (LogTool.gWorkEnable) LogTool.log.Info("磁盘空间回收:" + di_dd.FullName);
                                                                        }
                                                                        catch { }
                                                                    }
                                                                }
                                                                catch { }
                                                                #endregion
                                                            }
                                                        }

                                                    }
                                                    catch { }

                                                    try
                                                    {
                                                        di_mm.Delete();//删除空月
                                                    }
                                                    catch { }
                                                    #endregion
                                                }
                                            }
                                        }
                                        catch { }
                                        try
                                        {
                                            di_yyyy.Delete();//删除空年
                                        }
                                        catch { }
                                        #endregion
                                    }
                                }
                            }
                            catch { }
                            #endregion
                        }
                    }
                }
            }
            catch
            {
                ;
            }

        }


        /// <summary>
        /// 删除过期文件夹和文件(路径规则必须为：年月日/路口名称)
        /// 删除时找到年月日文件夹，如果过期，删除旗下所有图片和空文件夹
        /// </summary>
        private void DoDiscRecyleC(object obj)
        {
            try
            {
                TimeSpan fileKeepDays = TimeSpan.FromDays(m_keepdays);
                DateTime thistime = DateTime.Now;

                if ((m_path != "") && (Directory.Exists(m_path)))
                {
                    DirectoryInfo di_path = new DirectoryInfo(m_path);
                    DirectoryInfo[] di_yyyymmdds = di_path.GetDirectories();
                    if (di_yyyymmdds != null)
                    {
                        foreach (DirectoryInfo di_yyyymmdd in di_yyyymmdds)
                        {
                            #region try each yyyymmdd
                            try
                            {
                                string yyyy = di_yyyymmdd.Name.Substring(0, 4);
                                string mm = di_yyyymmdd.Name.Substring(4, 2);
                                string dd = di_yyyymmdd.Name.Substring(6, 2);
                                string strtime = yyyy + "-" + mm + "-" + dd + " 00:00:00";
                                DateTime time = DateTime.Parse(strtime);
                                if (thistime.Subtract(time).CompareTo(fileKeepDays) == 1)
                                {
                                    try
                                    {
                                        di_yyyymmdd.Delete(true);//删除yyyymmdd
                                        LogTool.log.Info("磁盘空间回收:" + di_yyyymmdd.FullName);
                                        di_yyyymmdd.Delete();
                                        LogTool.log.Info("删除空文件夹:" + di_yyyymmdd.FullName);
                                    }
                                    catch { }
                                }
                            }
                            catch { }
                            
                            #endregion
                        }
                    }
                }
            }
            catch
            {
                ;
            }

        }

    }
}
